/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.wii.elpro.web.action;

import com.wii.elpro.dao.hb.CabangDAOImpl;
import com.wii.elpro.dao.hb.DepartemenDAOImpl;
import com.wii.elpro.dao.hb.UserDAOImpl;
import com.wii.elpro.entity.User;
import com.wii.elpro.web.form.UserUpdateForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Vyor
 */
public class UserPreUpdateAction extends Action {

    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        HttpSession sess = request.getSession(true);
        User user = (User)sess.getAttribute("userSession");       
        if(user==null){
            return mapping.findForward("gagalLogin");            
        }  
        String forward = "";
        try {
            User u = new UserDAOImpl().getUser(Long.parseLong(request.getParameter("id")));
            UserUpdateForm f = (UserUpdateForm) form;
            f.setAlamat(u.getAlamat());
            f.setEmail(u.getEmail());
            f.setId(u.getId());
            if(u.getCabang()!=null)
                f.setIdCabang(u.getCabang().getId());
            if(u.getDepartemen()!=null)
                f.setIdDepartemen(u.getDepartemen().getId());
            f.setNama(u.getNama());
            f.setPassword(u.getPassword());
            f.setPasswordRetype(u.getPassword());
            f.setTelp(u.getTelp());
            f.setUsername(u.getUsername());
            request.setAttribute("departemens", new DepartemenDAOImpl().getDepartemens());
            request.setAttribute("cabangs", new CabangDAOImpl().getCabangs());
            forward = "success";
        } catch (Exception ex) {
            forward = "fail";
            ex.printStackTrace();
        }

        return mapping.findForward(forward);
    }
}
